<template>
  <div class="layout-container">
    <el-container>
      <el-aside width="220px" class="aside">
        <div class="logo">
          <h2>在线教育平台</h2>
        </div>
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical"
          router
          background-color="#001529"
          text-color="#fff"
          active-text-color="#409EFF"
        >
          <el-menu-item index="/student/dashboard">
            <el-icon><HomeFilled /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/student/homework">
            <el-icon><Document /></el-icon>
            <span>我的作业</span>
          </el-menu-item>
          <el-menu-item index="/student/messages">
            <el-icon><ChatLineRound /></el-icon>
            <span>消息中心</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header class="header">
          <div class="header-left">
            <el-icon class="toggle-btn" @click="toggleCollapse"><Expand /></el-icon>
          </div>
          <div class="header-right">
            <el-dropdown trigger="click" @command="handleCommand">
              <div class="user-info">
                <el-avatar size="small" :src="avatarUrl"></el-avatar>
                <span class="username">{{ userStore.realName }}</span>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                  <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { ElMessageBox } from 'element-plus';
import { HomeFilled, Document, ChatLineRound, Expand } from '@element-plus/icons-vue';
import { useUserStore } from '../../store/user';

const route = useRoute();
const router = useRouter();
const userStore = useUserStore();

// 折叠状态
const isCollapse = ref(false);

// 当前激活的菜单项
const activeMenu = computed(() => route.path);

// 头像URL
const avatarUrl = computed(() => {
  console.log('导航栏中的头像URL:', userStore.avatar);
  return userStore.avatar;
});

// 组件挂载时
onMounted(() => {
  console.log('Layout挂载，当前用户信息:', userStore.userInfo);
});

// 切换侧边栏折叠状态
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value;
};

// 处理下拉菜单命令
const handleCommand = (command) => {
  console.log('下拉菜单命令：', command);
  if (command === 'logout') {
    ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      userStore.logout();
    }).catch(() => {});
  } else if (command === 'profile') {
    // 跳转到个人信息页面
    console.log('正在跳转到学生个人信息页面...');
    router.push('/student/profile');
    console.log('跳转指令已发送');
  }
};
</script>

<style scoped>
.layout-container {
  height: 100vh;
  width: 100%;
}

.aside {
  background-color: #001529;
  color: white;
  overflow: hidden;
  height: 1200px;
}

.logo {
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.logo h2 {
  margin: 0;
  font-size: 18px;
  color: white;
}

.el-menu-vertical {
  border-right: none;
}

.header {
  background-color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #eee;
  padding: 0 20px;
}

.toggle-btn {
  font-size: 20px;
  cursor: pointer;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin-left: 8px;
}

.el-container {
  width: 100%;
}

.el-main {
  width: 100%;
  padding: 20px;
}
</style> 